// pages/index/recharge/recharge.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current:"1",
    money:"10",
  },
  //选择改变充值金额
  changeMoney(e){
    var current = e.currentTarget.dataset.current;
    var money = e.currentTarget.dataset.money;
    this.setData({
      current,
      money
    })
  },
  //输入自定义金额
  moneyInput(e) {
    let price = e.detail.value;
    price = price.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
    price = price.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
    price = price.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    price = price.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    // 去除整数前的0和小数点后多余的0，如011.020300 =》 11.0203
    const reg1 = /0*([1-9]\d*|0\.\d+)/;
    const reg2 = /(?:\.0*|(\.\d+?)0+)$/;
    price = price.replace(reg1, '$1').replace(reg2, '$1');
     this.setData({
       money:price
     })
   },
   //submit 充值余额提交
   submit(){
    let form = {userId:wx.getStorageSync('userId'),block:'余额充值',info:'微信支付',money:this.data.money?this.data.money:0}
    app.$http.put('/app/user/recharge',form).then(res=>{
      setTimeout(function() {
        wx.navigateBack()
      }, 1000);
      wx.showToast({
        title: '充值成功',
        icon:'success',
        duration:1000
      })
    }).catch(err=>{
      console.log(err)
    })
   }
})